package shuaige.jdk.多线程.锁.可重入锁;


import java.util.concurrent.locks.ReentrantLock;

public class ReentrantLock_示例1 implements Runnable {
    // 创建锁对象
    public static ReentrantLock lock = new ReentrantLock();
    private int thold;

    public ReentrantLock_示例1(int h) {
        this.thold = h;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 5; i++) {
            new Thread(new ReentrantLock_示例1(i), "name-" + i).start();
        }
    }

    @Override
    public void run() {
        try {
            Thread.sleep(100);
            lock.lock(); //获取锁
            System.out.print("厨师:" + Thread.currentThread().getName());
            System.out.print("给客人" + thold + "：");
            for (int i = 0; i < 15; i++) {
                Thread.sleep(100);
                System.out.print(thold + ",");
            }
        } catch (InterruptedException e) {
            System.out.println("InterruptedException");
        } finally {
            System.out.println(" 完成");
            lock.unlock();
        }
    }

}